package com.futuremind.recyclerviewfastscroll.example.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import com.futuremind.recyclerviewfastscroll.example.Log;
import com.futuremind.recyclerviewfastscroll.example.ResourceTable;
import com.futuremind.recyclerviewfastscroll.example.adapters.PageProvider;
import com.futuremind.recyclerviewfastscroll.example.adapters.ViewCreateHelper;

import java.io.IOException;
import java.util.ArrayList;

/**
 * MainAbilitySlice
 *
 * @since 2021-04-10
 */
public class MainAbilitySlice extends AbilitySlice implements TabList.TabSelectedListener,
    PageSlider.PageChangedListener {
    /**
     * 格式化
     */
    private String format = "%s";
    private TabList tabList;
    private PageSlider pageSlider;
    private int[] resLayout = new int[]{ResourceTable.Layout_page_default, ResourceTable.Layout_page_styled,
        ResourceTable.Layout_page_horizontal, ResourceTable.Layout_page_default};
    private final int leftPadding = 7;
    private final int rightPadding = 5;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_rootPage);
    }

    private void initListener() {
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                if (pageSlider.getCurrentPage() != tab.getPosition()) {
                    pageSlider.setCurrentPage(tab.getPosition());
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
            }

            @Override
            public void onReselected(TabList.Tab tab) {
            }
        });

        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
            }

            @Override
            public void onPageSlideStateChanged(int state) {
            }

            @Override
            public void onPageChosen(int itemPos) {
                if (tabList.getSelectedTab().getPosition() != itemPos) {
                    tabList.selectTabAt(itemPos);
                }
            }
        });
    }

    private void initData() {
        String[] titles = getStringResource(ResourceTable.Strarray_title_array);
        for (int i = 0; i < titles.length; i++) {
            TabList.Tab tab = tabList.new Tab(getContext());
            tab.setText(titles[i]);
            tab.setPadding(AttrHelper.fp2px(leftPadding, this), 0, AttrHelper.fp2px(rightPadding, this), 0);
            tabList.addTab(tab);
        }
        tabList.selectTabAt(0);
        pageSlider.setProvider(new PageProvider(initPageSliderViewData()));

    }

    private ArrayList<Component> initPageSliderViewData() {
        int pageSize = tabList.getTabCount();
        ArrayList<Component> pages = new ArrayList<>();
        ViewCreateHelper viewCreateHelper = new ViewCreateHelper(this);
        for (int i = 0; i < pageSize; i++) {
            pages.add(viewCreateHelper.createView(resLayout[i], i));
        }
        return pages;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onSelected(TabList.Tab tab) {
    }

    @Override
    public void onUnselected(TabList.Tab tab) {
    }

    @Override
    public void onReselected(TabList.Tab tab) {
    }

    @Override
    public void onPageSliding(int i, float v, int i1) {
    }

    @Override
    public void onPageSlideStateChanged(int i) {
    }

    @Override
    public void onPageChosen(int i) {
    }

    /**
     * 获取资源文件数据
     *
     * @param res 资源Id
     * @return 资源数据数组
     */
    public String[] getStringResource(int res) {
        try {
            String[] result = getResourceManager().getElement(res).getStringArray();
            return result;
        } catch (IOException e) {
            Log.e(format, e.getMessage());
        } catch (NotExistException e) {
            Log.e(format, e.getMessage());
        } catch (WrongTypeException e) {
            Log.e(format, e.getMessage());
        }
        return new String[]{};
    }
}
